namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceAgenciesPage : Page
    {
        protected Image iResource;
        protected Button newBtn;
        protected UserPrincipal Principal = null;
        protected DataGrid ResourceAgenciesDg;

        private void FillPage()
        {
            this.ResourceAgenciesDgDataBind();
        }

        private void InitializeComponent()
        {
            this.newBtn.Click += new EventHandler(this.newBtn_Click);
            this.ResourceAgenciesDg.ItemCommand += new DataGridCommandEventHandler(this.ResourceAgenciesDg_ItemCommand);
            this.ResourceAgenciesDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.ResourceAgenciesDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.ResourceAgenciesDg.PageSize = 14;
        }

        private bool IsDelete()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
                return false;
            }
            return (this.Principal.OwnRole.TypeC == 1);
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("ResourceAgenciesPageEdit.aspx", true);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void ResourceAgenciesDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strAgenciesId = e.CommandArgument.ToString();
            Guid agenciesId = new Guid();
            try
            {
                agenciesId = new Guid(strAgenciesId);
            }
            catch (Exception)
            {
                return;
            }
            string str = cmd;
            if (str != null)
            {
                str = string.IsInterned(str);
                if (str == "Edit")
                {
                    base.Response.Redirect("ResourceAgenciesPageEdit.aspx?IsModify=True&AgenciesId=" + agenciesId.ToString(), true);
                }
                else if ((str == "delete") && this.IsDelete())
                {
                    ManageResource.RemoveResourceAgenciesInfoViaId(agenciesId);
                    if ((this.ResourceAgenciesDg.Items.Count == 1) && (this.ResourceAgenciesDg.CurrentPageIndex > 0))
                    {
                        this.ResourceAgenciesDg.CurrentPageIndex--;
                    }
                    this.ResourceAgenciesDgDataBind();
                }
            }
        }

        private void ResourceAgenciesDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.ResourceAgenciesDg.CurrentPageIndex = e.NewPageIndex;
            this.ResourceAgenciesDgDataBind();
        }

        private void ResourceAgenciesDgDataBind()
        {
            if (base.Request.QueryString["IsSearch"] == "True")
            {
                Guid ProvinceId = new Guid(this.Page.Session["ProvinceId"].ToString());
                Guid CityId = new Guid(this.Page.Session["CityId"].ToString());
                string strKey = this.Page.Session["Key"].ToString();
                if (ManageProvince.IsExistCityInProvinceViaProvinceId(ProvinceId) && (CityId.ToString() == "00000000-0000-0000-0000-000000000001"))
                {
                    this.ResourceAgenciesDg.DataSource = ManageResource.GetResourceAgenciesInfo(ProvinceId, strKey);
                    this.ResourceAgenciesDg.DataBind();
                }
                else
                {
                    try
                    {
                        if (ProvinceId.Equals(new Guid()))
                        {
                            this.ResourceAgenciesDg.DataSource = ManageResource.GetResourceAgenciesInfo(strKey);
                        }
                        else
                        {
                            this.ResourceAgenciesDg.DataSource = ManageResource.GetResourceAgenciesInfo(ProvinceId, CityId, strKey);
                        }
                        this.ResourceAgenciesDg.DataBind();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                this.ResourceAgenciesDg.DataSource = ManageResource.GetResourceAgenciesInfo();
                this.ResourceAgenciesDg.DataBind();
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.newBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.newBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "newBtn", language, path);
            this.ResourceAgenciesDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "name", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.ResourceAgenciesDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "provinceOverSea", language, path);
                this.ResourceAgenciesDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "cityOverSea", language, path);
            }
            else
            {
                this.ResourceAgenciesDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "province", language, path);
                this.ResourceAgenciesDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "city", language, path);
            }
            this.ResourceAgenciesDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "edit", language, path);
            this.ResourceAgenciesDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPage", "delete", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

